
var express = require('express')

var url = require('url')

var router = express.Router()  /*可使用express.Router类创建模块化,可挂载的路由句柄*/

var login = require('./admin/login')
var product = require('./admin/product')
var user = require('./admin/user')

//设置中间件，判断登录状态
router.use(function (req, res, next) {

    // console.log('path is',req.url)

    var pathName = url.parse(req.url).pathname

    if (pathName == '/login' || pathName == '/login/doLogin') {
        next()
    }else {
        if (req.session.userInfo&&req.session.userInfo.userAccount!='') {//判断是否有登录

            //app.locals 全局
            //
            //req.app.locals /*请求的全局*/
            req.app.locals['userName'] = req.session.userInfo.userAccount

            next()
        }else {
            console.log('跳到登录页')
            res.redirect('/admin/login')
        }
    }
})
//配置路由
router.use('/login',login)
router.use('/product',product)
router.use('/user',user)


module.exports = router